package com.javapandeng.analys;

import org.apache.log4j.Logger;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author maciqing
 * @create 2021-02-15 12:49
 */
public class CounterServlet extends HttpServlet {
    private static Logger logger = Logger.getLogger(CounterServlet.class);
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        ServletContext context = getServletContext();
        Integer count = null;
        synchronized(context)
        {
            count = (Integer) context.getAttribute("counter");
            if (null == count)
            {
                count = new Integer(1);
            }
            else
            {
                count = new Integer(count.intValue() + 1);
            }
            context.setAttribute("counter", count);
        }

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        /*out.println("<html><head>");
        out.println("<title>页面访问统计</title>");
        out.println("</head><body>");
        out.println("该页面已被访问了" + "<b>" + count + "</b>" + "次");
        out.println("<script>alert('弹窗要显示的内容！'+ count +);</script>");
        out.println("</body></html>");*/
        out.close();
        logger.info(count);

    }

}
